package com.edu.edusystem.controller;


import com.edu.edusystem.domain.Course;
import com.edu.edusystem.domain.CourseLesson;
import com.edu.edusystem.domain.CourseSection;
import com.edu.edusystem.domain.ResponseResult;
import com.edu.edusystem.service.CourseContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/courseContent")
public class CourseContentController {

    @Autowired
    private CourseContentService courseContentService;
    /**
     * 查询课程内容
     * */
    @RequestMapping("/findSectionAndLesson")
    public ResponseResult findSectionAndLessonByCourseId(@RequestParam int courseId){
            //调用service
            List<CourseSection> sectionList = courseContentService.findSectionAndLessonByCourseId(courseId);
            //封装数据并返回
            ResponseResult responseResult = new ResponseResult(true,200,"响应成功",sectionList);
            return responseResult;

    }

    /**
     * 回显章节对应的课程信息
     * */
    @RequestMapping("/findCourseByCourseId")
    public ResponseResult findCourseByCourseId( Integer courseId){

            //调用service
            Course course = courseContentService.findCourseByCourseId(courseId);
            ResponseResult responseResult = new ResponseResult(true,200,"响应成功",course);
            return responseResult;
    }

    /**
     *新增&更新章节信息
     */
    @RequestMapping("/saveOrUpdateSection")
    public ResponseResult saveOrUpdateSection(@RequestBody CourseSection courseSection) {

        // 判断是否携带了章节ID
        if(courseSection.getId() == null){
            // 新增
            courseContentService.saveSection(courseSection);
            return new ResponseResult(true,200,"新增章节成功",null);
        }else {
            // 更新
            courseContentService.updateSection(courseSection);
            return new ResponseResult(true,200,"更新章节成功",null);
        }
    }

    /**
     * 修改章节状态
     * 状态，0:隐藏；1：待更新；2：已发布
     * */
    @RequestMapping("/updateSectionStatus")
    public ResponseResult updateSectionStatus( int id, int status){

            courseContentService.updateSectionStatus(id,status);

            //数据响应
            Map<String,Integer> map = new HashMap<>();
            map.put("status",status);
        return new ResponseResult(true,200,"响应成功",map);
    }

    /**
     * 修改课时状态
     * 状态，0:隐藏；1：待更新；2：已发布
     * */
    @RequestMapping("/updateLessonStatus")
    public ResponseResult updateLessonStatus( int id, int status){


        courseContentService.updateLessonsStatus(id,status);

        //数据响应
        Map<String,Integer> map = new HashMap<>();
        map.put("status",status);
        return new ResponseResult(true,200,"响应成功",map);
    }

    /**
     * 保存&修改课时
     * */
    @RequestMapping("/saveOrUpdateLesson")
    public ResponseResult saveOrUpdateLesson(@RequestBody CourseLesson lesson){
        try {
            if(lesson.getId() == null){
                courseContentService.saveLesson(lesson);
                return new ResponseResult(true,200,"响应成功",null);
            } else {
                courseContentService.updateLesson(lesson);
                return new ResponseResult(true,200,"响应成功",null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
